package com.example.web;

import com.example.util.Activiti7Util;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.*;

/**
 * [描述]
 *@author Da.Pang
 */
@Controller
@RequestMapping("/act")
public class ActivityController {
    @Resource
    private Activiti7Util util;

    /**
     * [描述] bpmn文件中的主id
     */
    public static String bpmnProcessId = "Process_test";
    public static Map<String, String> map = new HashMap<>();
    public static String assigneeName = "小明";

    List<String> banzhangList = Arrays.asList("小黑班长","小白班长");
    List<String> teacherList = Arrays.asList("老师A","老师B");
    List<String> fuList = Arrays.asList("小粉副校长","小红副校长");
    List<String> zhengList = Arrays.asList("小绿正校长","小蓝正校长");


    /**
    * [描述] 部署bpmn文件
    *@author Da.Pang
    */
   @RequestMapping("/bushu")
   @ResponseBody
   public Object bushu( @RequestParam Map<String, Object> params ){
       String name = "请假条";
       String bpmnName = "qingjia.bpmn";
       String bpmnImgName = "qingjia.svg";
       Map<String, Object> deployment = util.importProcessByResources(name, bpmnName, bpmnImgName);
       return deployment;
   }

    /**
     * [描述] 启动一个流程实例
     *@author Da.Pang
     */
    @RequestMapping("/kaishi")
    @ResponseBody
    public Object kaishi(String applyUserName){

        Map<String, Object> params = new HashMap<>();
        params.put("userName", applyUserName);
        params.put("banzhangList", banzhangList);
        params.put("teacherList", teacherList);
        params.put("fuList", fuList);
        params.put("zhengList", zhengList);

        //TODO 这里可加入业务表 并获得新增的业务id  传入 businessKey
        String businessKey = "这里是业务id";
        util.startProcessInstanceByKey(bpmnProcessId, businessKey, params);
        List<Map<String, Object>> tasks = util.taskListByMy(assigneeName, bpmnProcessId, 0, 100);
        for (Map<String, Object> task : tasks) {
            String taskId = (String) task.get("taskId");
            //开始一个流程 就先完成自己的任务
            util.completeTask(assigneeName , taskId,null);
        }
        return "success";
    }

    /**
     * 这里传入某个候选人 获取自己的待办任务
     *@author Da.Pang
     */
    @RequestMapping("/shiquList")
    @ResponseBody
    public Object shiquList( @RequestParam Map<String, Object> params ){
        String candidateUserName = "SYSTEM";
        return util.taskListByCandidateUser(bpmnProcessId, candidateUserName);
    }



    /**
     * [描述] 拾取任务，并操作任务是否通过
     *@author Da.Pang
     */
    @RequestMapping("/tongguo")
    @ResponseBody
    public Object tongguo( @RequestParam Map<String, Object> params ){
        String taskId = "";
        params.put("roomCheck", 1);
        String candidateUserName = "SYSTEM";
        return  util.claimTaskAndCompleteTask(candidateUserName, taskId, params);
    }
    /**
     * [描述] 查已经操作过的任务、历史信息
     *@author Da.Pang
     */
    @RequestMapping("/lishi")
    @ResponseBody
    public Object historicActivityInstanceList( @RequestParam Map<String, Object> params ){
        String definitionId = "resourcesOccupy3:2:bb047ad5-9ecd-11ec-a39b-080058000005";
        String instanceId = null;
        String assigneeName = "";
        return  util.historicActivityInstanceList(definitionId, instanceId , assigneeName);
    }

}
